package org.come.Jpanel;

import java.awt.image.ColorConvertOp;
import java.awt.color.ColorSpace;
import java.awt.image.BufferedImage;
import java.awt.image.ImageObserver;
import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import java.awt.Graphics;
import javax.swing.BorderFactory;
import javax.swing.plaf.ScrollBarUI;

import org.come.until.SrcollPanelUI;

import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.TreePath;
import org.come.Frame.FriendMsgJframe;
import org.come.bean.ChatBean;
import org.come.until.FormsManagement;
import org.come.until.MessagrFlagUntil;
import org.come.Frame.FriendChatMessageJframe;
import javax.swing.SwingUtilities;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.tree.TreeCellRenderer;
import org.come.entity.Friendtable;
import org.come.until.UserMessUntil;
import com.tool.tcpimg.UIUtils;
import com.updateNew.MyIsif;

import javax.swing.Icon;
import org.come.until.CutButtonImage;
import java.awt.Component;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.plaf.basic.BasicTreeUI;
import javax.swing.tree.TreeNode;
import org.come.until.JTreeData;
import java.awt.Color;
import java.awt.Dimension;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import javax.swing.ImageIcon;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.JTree;
import javax.swing.ScrollPaneConstants;

import com.tool.btn.SmallIconBtn;
import com.tool.image.ImageMixDeal;

import javax.swing.JScrollPane;
import javax.swing.JPanel;



/**
 *  好友列表主面板
 * @author Administrator
 *
 */


public class TestfriendlistJapnel extends JPanel {
    private JScrollPane jScrollPane;
    private SmallIconBtn btnAdd;
    private SmallIconBtn btnCha;
    private JTree jTree;
    private DefaultMutableTreeNode top;
    private ImageIcon icon;


    public TestfriendlistJapnel() throws Exception {


        	this.setPreferredSize(new Dimension(158, 498));
    		this.setBackground(Color.BLACK);
    		this.setLayout(null);
//    		// 关闭按钮事件
//    		FormsOnOffBtn offBtn = new FormsOnOffBtn("inkImg/hongmu/2/icon_close.png", 1, 4);
//    		offBtn.setBounds(171, 0, 19, 20);
//    		this.add(offBtn);

    		top = new DefaultMutableTreeNode("");
    		// 循环放上好友列表的方法
    		JTreeData.ShowFriendMsg(top, jTree);

    		// 套装列表
    		jTree = new JTree(top);
    		jTree.setOpaque(false);
    		jTree.putClientProperty("JTree.lineStyle", "None"); // 去除jtree的线条
    		((BasicTreeUI) jTree.getUI()).setLeftChildIndent(0); // 设置父节点与子节点左对齐，并去除缩进
    		DefaultTreeCellRenderer cellRenderer = new DefaultTreeCellRenderer() {
    			@Override
    			public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected, boolean expanded, boolean leaf,
    					int row, boolean hasFocus) {
    				DefaultMutableTreeNode node = (DefaultMutableTreeNode) value;
    				super.getTreeCellRendererComponent(tree, value, selected, expanded, leaf, row, hasFocus);
    				// 判断是几级的叶子
    				if (node.getLevel() == 2) {
    					JTreeData jtd = (JTreeData) node.getUserObject();
    					setIcon(CutButtonImage.getImage(jtd.getPath(), 20, 20));
    					setText(jtd.getString());
    					setFont(UIUtils.TEXT_FONT);
    					setForeground(Color.green);
    					if (UserMessUntil.getFriendtables() != null && UserMessUntil.getFriendtables().size() > 0) {
    						// 根据在线不在线区分名字颜色
    						for (int i = 0; i < UserMessUntil.getFriendtables().size(); i++) {
    							Friendtable friend = UserMessUntil.getFriendtables().get(i);
    							if (friend.getRolename().equals(jtd.getString())) {
    								if (friend.getOnlineState() == 1) {// 在线
    									setForeground(Color.green);
    								} else {
    									setForeground(Color.gray);
    								}
    							}
    						}
    					}
    				} else {
    					if ("大话精灵".equals(node.getUserObject())) {
    						setForeground(Color.GREEN);
    						setIcon(CutButtonImage.getImage("img/head/999999.png", 25, 25));
    					} else {
    						setForeground(Color.WHITE);
    					}
    					setFont(UIUtils.TEXT_HURT2);
    				}

    				return this;
    			}
    		};
    		cellRenderer.setLeafIcon(null); // 设置叶子图标
    		cellRenderer.setOpenIcon(new ImageIcon("inkImg/hongmu/2/expand.png")); // 设置打开子节点图标
    		cellRenderer.setClosedIcon(new ImageIcon("inkImg/hongmu/2/not_expanded.png")); // 设置关闭子节点图标

    		cellRenderer.setFont(UIUtils.TEXT_HURT2);// 设置字体.
    		cellRenderer.setBackgroundNonSelectionColor(new Color(0, 0, 0, 0));// 设置非选定节点的背景色
    		cellRenderer.setBackgroundSelectionColor(new Color(99, 93, 90));// 设置节点在选中状态下的背景色
    		cellRenderer.setBorderSelectionColor(new Color(0, 0, 0, 0));// 设置选中状态下节点边框的颜色

    		/*
    		 * 设置选时或不选时，文字的变化颜色
    		 */
    		cellRenderer.setTextNonSelectionColor(Color.white);// 设置绘制未选中状态下节点文本的颜色
    		cellRenderer.setTextSelectionColor(Color.white);// 设置绘制选中状态下节点文本的颜色
    		jTree.setCellRenderer(cellRenderer);
    		jTree.setRootVisible(false); // 隐藏根节点
    		jTree.setRowHeight(30); // 设置节点行高
    		// 单击展开子节点
    		jTree.addMouseListener(new MouseListener() {
    			@Override
    			public void mouseReleased(MouseEvent e) {
    				DefaultMutableTreeNode selectionNode = (DefaultMutableTreeNode) jTree.getLastSelectedPathComponent();
    				if (SwingUtilities.isLeftMouseButton(e)) { // 点击了鼠标左键
    					TreePath path = jTree.getSelectionPath();
    					if (path != null) {
    						if (jTree.isExpanded(path)) {
    							jTree.collapsePath(path);// 关闭节点
    						} else {
    							jTree.expandPath(path);// 展开节点
    						}
    					}
    					if (e.getClickCount() == 2) {
    						if (selectionNode.getLevel() == 2) {// 判断是否是叶子节点
    							JTreeData jtd = (JTreeData) selectionNode.getUserObject();
    							// 选中的人物发送信息
    							for (int i = 0; i < UserMessUntil.getFriendtables().size(); i++) {
    								Friendtable friend = UserMessUntil.getFriendtables().get(i);
    								if (friend.getRolename().equals(jtd.getString())) {
    									FriendChatMessageJframe.getFriendChatMessageJframe().getJpanel()
    											.showFriend(friend, MessagrFlagUntil.getRichLabel(jtd.getString()));
    									// 打开聊天面板
    									FormsManagement.showForm(56);
    									break;
    								}
    							}
    						} else {
                                final String nodeName = selectionNode.toString();
                                final JTreeData jtd = new JTreeData(nodeName,"img/head/999999.png");
                                Friendtable friend = new Friendtable();
                                friend.setRole_id(new BigDecimal(99999));
                                friend.setRolename(nodeName);
                                friend.setOnlineState(1);
                                friend.setGrade(new BigDecimal(1));
                                friend.setSpecies_id(new BigDecimal(999999));
                                if ("大话精灵".equals(nodeName)) {
                                	FriendChatMessageJframe.getFriendChatMessageJframe().getJpanel().showFriend(friend, MessagrFlagUntil.getRichLabel(jtd.getString()));
                                    FormsManagement.showForm(56);
                                }
                            }
    					}
    				} else if (SwingUtilities.isRightMouseButton(e)) {
    					if (selectionNode.getLevel() == 2) {
    						JTreeData jtd = (JTreeData) selectionNode.getUserObject();
    						if (UserMessUntil.getFriendtables() == null) {
                                return;
                            }
    						// 选中的人物发送信息
    						for (int i = 0; i < UserMessUntil.getFriendtables().size(); i++) {
    							Friendtable friend = UserMessUntil.getFriendtables().get(i);
    							if (friend.getRolename().equals(jtd.getString())) {
    								FriendMsgJframe.getFriendMsgJframe().getMsgJpanel().show(friend);
    								// 弹出好友信息面板
    								FormsManagement.showForm(76);
    								break;
    							}
    						}
    					}
    				}
    			}

    			@Override
    			public void mousePressed(MouseEvent e) {
    			}

    			@Override
    			public void mouseExited(MouseEvent e) {
    			}

    			@Override
    			public void mouseEntered(MouseEvent e) {
    			}

    			@Override
    			public void mouseClicked(MouseEvent e) {
    			}
    		});
    		jTree.addTreeSelectionListener(new TreeSelectionListener() {

    			@Override
    			public void valueChanged(TreeSelectionEvent e) {

    				JTree tree = (JTree) e.getSource();
    				// 利用JTree的getLastSelectedPathComponent()方法取得目前选取的节点.
    				DefaultMutableTreeNode selectionNode = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent();

    			}
    		});

    		jScrollPane = new JScrollPane(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
    		jScrollPane.getVerticalScrollBar().setUI(new SrcollPanelUI());
    		jScrollPane.getViewport().setOpaque(false);
    		jScrollPane.setOpaque(false);
    		jScrollPane.setBorder(BorderFactory.createEmptyBorder());
    		jScrollPane.setViewportView(jTree);
    		jScrollPane.setBounds(5, 74, 155, 395);
    		this.add(jScrollPane);

    		// 添加好友列表的按钮
    		btnAdd = new SmallIconBtn("skin/8100226/button/B35.png", 1, 100, "添加", null);
    		btnAdd.setBounds(5, 475, 19, 20);
    		this.add(btnAdd);

    		// 查找好友的按钮
    		btnCha = new SmallIconBtn("skin/8100226/button/B40.png", 1, 101, "查找", null);
    		btnCha.setBounds(25, 475, 19, 20);
    		this.add(btnCha);
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        	// 好友列表
    		if (icon == null) {
                if("水墨UI".equals(MyIsif.getStyle())) {
					icon = new ImageIcon("inkImg/newbackground/B215.png");
                }else{
					icon = new ImageIcon("inkImg/hongmu/B215h.png");
                }
            }

    		g.drawImage(icon.getImage(), 0, 0, 158, 498, this);
    }

    
    public static BufferedImage getGrayImage(final BufferedImage originalImage) {
        final int imageWidth = originalImage.getWidth();
        final int imageHeight = originalImage.getHeight();
        final BufferedImage grayImage = new BufferedImage(imageWidth, imageHeight, 5);
        final ColorConvertOp cco = new ColorConvertOp(ColorSpace.getInstance(1003), null);
        cco.filter(originalImage, grayImage);
        return grayImage;
    }

    public JScrollPane getjScrollPane() {
        return this.jScrollPane;
    }
    
    public void setjScrollPane(final JScrollPane jScrollPane) {
        this.jScrollPane = jScrollPane;
    }
    
    public DefaultMutableTreeNode getTop() {
        return this.top;
    }
    
    public void setTop(final DefaultMutableTreeNode top) {
        this.top = top;
    }
    
    public JTree getjTree() {
        return this.jTree;
    }
    
    public void setjTree(final JTree jTree) {
        this.jTree = jTree;
    }


}
